<?php

/**
 * @category   MM
 * @package    MM_Service
 */
class MM_Service_Admin_EventUserComment extends Mediad2_Service_Abstract
{
	/**
	 * 初始化
	 */
	public function init()
	{
		$this->setMapper('MM_Mapper_EventUserComment');
		$this->setModel('MM_Model_EventUserComment');
	}
	
	/**
	 * 评论/回复评论
	 * @param string $commentType 
	 * @param int $eid
	 * @param int $uid
	 * @param string $objecttype
	 * @param int $objectid
	 * @param string $content
	 * @param int $reply_eucid
	 * @return Mediad2_Model_Result
	 */
	public function createComment($commentType='text', $eid, $uid, $objecttype, $objectid, $content='', $reply_eucid = 0)
	{
		//事务处理
		$db1 = Zend_Registry::get('db1');
		$db1->beginTransaction();
	
		try {
			
			//如果是语音评论，则需要先处理上传的语音文件
			$voice_path = null;
			switch ($commentType) {
				case "voice":
					$sourcetype = Mediad2_Model_Files::SOURCETYPE_PHONE;
					$sourceid 	= $uid;
					$upload_name = 'Filedata';
					
					//上传文件接收和处理
					$serviceFiles = new Mediad2_Service_Admin_Files();
					$result = $serviceFiles->upload($sourcetype, $sourceid, $upload_name);
					
					//处理文件出现失败，则终止处理
					if ($result->getCode() == $result::CODE_FAILURE) {
						return $result;
					}
					
					//处理上传文件正常，则继续
					//获取fid
					$files = $result->getData();
					$voice_path = $files->getFilepath();
					$content = '';
					break;
				case "text":
				default:
					$voice_path = null;
					break;
			}
			
			//新增一条评论表记录
			$model = new MM_Model_EventUserComment();
			$model->setEid($eid);
			$model->setUid($uid);
			$model->setAddtime(time());
			$model->setObjecttype($objecttype);
			$model->setObjectid($objectid);
			$model->setReply_eucid($reply_eucid);
			$model->setReply_uid(0);
			$model->setContent($content);
			$model->setIsdel($model::ISDEL_NO);
			$model->setDeltime(0);
			$model->setVoice_path($voice_path);
			$this->getMapper()->insert($model->toArray());
	
			switch ($objecttype) {
				case 'event_files':
					//更新files表，设置commentcount+1
					$where = "fid = $objectid";
					$row = array(
							'commentcount' => new Zend_Db_Expr("commentcount + 1"),
					);
					$serviceFiles = new MM_Service_Admin_EventFiles();
					$serviceFiles->getMapper()->__updateSQL($row, $where);
					break;
				default:
					break;
			}
			
			//提交事务
			$db1->commit();
	
			//返回结果--成功
			return $this->returnResult(Mediad2_Model_Result::CODE_SUCCESS);
	
		} catch (Zend_Exception $e) {
	
			//回滚事务
			$db1->rollback();
	
			//出错处理
			$this->handleException($e);
	
			//返回结果--失败
			return $this->returnResult(Mediad2_Model_Result::CODE_FAILURE);
		}
	}
	
	/**
	 * 删除评论
	 * @param int $uid
	 * @param int $eucid
	 * @return Mediad2_Model_Result
	 */
	public function delComment($uid, $eucid)
	{
		//事务处理
		$db1 = Zend_Registry::get('db1');
		$db1->beginTransaction();
	
		try {
			//根据指定id，获得comment
			$result = $this->find($eucid);
			if ($result->getCode()==$result::CODE_FAILURE) {
				return $result;
			}
			$comment = $result->getData();
			
			//更新评论表，设置isdel=1
			$row = array(
					'isdel' => 1,
					'deltime' => time(),
			);
			$where = "isdel = 0 AND eucid = $eucid AND uid = $uid";
			$this->getMapper()->__updateSQL($row, $where);
	
			//根据评论对象的类型，更改评论数
			$objecttype = $comment->getObjecttype();
			$objectid	= $comment->getObjectid();
			
			switch ($objecttype) {
				case 'event_files':
					//更新files表，设置commentcount+1
					$where = "fid = $objectid";
					$row = array(
							'commentcount' => new Zend_Db_Expr("commentcount - 1"),
					);
					$serviceFiles = new MM_Service_Admin_EventFiles();
					$serviceFiles->getMapper()->__updateSQL($row, $where);
					break;
				default:
					break;
			}
	
			//提交事务
			$db1->commit();
	
			//返回结果--成功
			return $this->returnResult(Mediad2_Model_Result::CODE_SUCCESS);
	
		} catch (Zend_Exception $e) {
	
			//回滚事务
			$db1->rollback();
	
			//出错处理
			$this->handleException($e);
	
			//返回结果--失败
			return $this->returnResult(Mediad2_Model_Result::CODE_FAILURE);
		}
	}
	
}